package adminctl

import (
	"backenddev/shop_server/internal/defs"
	"backenddev/shop_server/internal/logics"
	"backenddev/shop_server/nets"
	reqs "backenddev/shop_server/requests"

	"github.com/gin-gonic/gin"
)

// 管理员登录
// @Summary 管理员登录
// @Description 管理员登录
// @Tags 管理员

func AdminLogin(c *gin.Context) {
	//获取前端登录参数
	req := reqs.AdminLoginReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		nets.Fail(c, defs.Error_ArgsError)
		return
	}
	user, err := logics.AdminLogin(req.Username, req.Password)
	if err != nil {
		nets.Fail(c, err)
		return
	}
	c.JSON(200, gin.H{"user": user})

}

// 获取用户列表 后台admin获取
func GetUserListByAdmin(c *gin.Context) {
	req := reqs.GetUsesrListReq{}
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
		return
	}
	userList, err := logics.GetUserList(req.PageNum, req.PageSize)
	if err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
	}
	c.JSON(200, gin.H{"user_list": userList})
}

// 查询单个用户详细信息
func QueryUserDetail(c *gin.Context) {
	req := reqs.GetUserDetailReq{}
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
		return
	}
	user, err := logics.GetUserDetail(req.Uid)
	if err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
	}
	c.JSON(200, gin.H{"user": user})
}

// 拉黑用户
func BlockUser(c *gin.Context) {
	req := reqs.BlockUserReq{}
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
		return
	}
	success, err := logics.BlockUser(req.Uid, req.Reason)
	if err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
	}
	c.JSON(200, gin.H{"success": success})
}

// 解禁用户
func UnBlockUser(c *gin.Context) {
	req := reqs.BlockUserReq{}
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
		return
	}
	success, err := logics.UnBlockUser(req.Uid)
	if err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
	}
	c.JSON(200, gin.H{"success": success})
}

// 统计活跃用户  日活跃用户  三日活跃用户  7日活跃用户
func GetActiveUser(c *gin.Context) {
	oneDayUsers, threeDayUsers, sevenDayUsers, err := logics.GetActiveUser()
	if err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
	}
	c.JSON(200, gin.H{"one_day_users": oneDayUsers, "three_day_users": threeDayUsers, "seven_day_users": sevenDayUsers})
}

// 搜索用户
func SearchUser(c *gin.Context) {
	req := reqs.SearchUserReq{}
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
		return
	}
	userList, err := logics.SearchUser(req.Uid, req.NickName, req.Phone)
	if err != nil {
		c.JSON(400, gin.H{"error": "Invalid input"})
	}
	c.JSON(200, gin.H{"user_list": userList})
}
